package exemplos.calculadora;

import java.util.ArrayList;

import javafx.application.Application;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.geometry.VPos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox;
import javafx.stage.Popup;
import javafx.stage.PopupWindow;
import javafx.stage.Stage;

public class Calculadora extends Application {

	private TextField visor;
	private ArrayList<Button> botoes = new ArrayList<Button>();
	private String[] stringBotoes = {"<", "C", "±", "√", "x²", "7", "8", "9", "/", "x³", "4", "5", "6", "*", "1/x",
			"1", "2", "3", "-", "=", "0", ",", "+" };
	private MenuBar menuBar;
	Scene sceneCalc;
	Scene sceneCalcHist;
	
	TableView<Operacao> tableView;
	TableColumn<Operacao, String> expCol;
	TableColumn<Operacao, String> resultadoCol;
	public void start(Stage primaryStage) throws Exception {
		VBox root = new VBox();
		GridPane grid = new GridPane();
		primaryStage.setTitle("Calculadora");
		sceneCalc = new Scene(root, 230, 250);
		primaryStage.setScene(sceneCalc);
		primaryStage.show();
		primaryStage.setResizable(false);
		root.setStyle("-fx-background-color:linear-gradient(rgba(250,250,250),rgba(220, 230, 230));");
		menuBar = new MenuBar();
		Menu menu = new Menu("Opções");
		menu.getItems().addAll(new MenuItem("Histórico"), new MenuItem("Fechar"));

		Menu menu2 = new Menu("Editar");
		menu2.getItems().addAll(new MenuItem("Copiar"), new MenuItem("Colar"));
		Menu menu3 = new Menu("Sobre");
		menu3.getItems().addAll(new MenuItem("Célula"));
		menuBar.getMenus().addAll(menu, menu2, menu3);
		visor = new TextField("0");
		visor.setFocusTraversable(false);
		visor.setEditable(false);
		visor.setAlignment(Pos.CENTER_RIGHT);
		
		visor.setStyle("-fx-text-fill: black;" +
				"    -fx-font-family: 'Consolas';" +
				"	 -fx-font-size: 22px;"	 +	
				"    -fx-background-color: linear-gradient(rgba(250,250,250),rgba(180, 200, 210));" + //#61a2b1, #2A5058
				"    -fx-effect: dropshadow( three-pass-box , rgba(0,0,80,0.6) , 5, 0.0 , 0 , 1 );");
		
		grid.setPadding(new Insets(5,10,10,10));
		grid.setVgap(7);
		grid.setHgap(6);
		int x = 0;
		int y = 2;
		visor.setPrefHeight(50);
		GridPane.setConstraints(visor, 0, 1, 5, 2);
		y++;
		for(int i=0; i < stringBotoes.length; i++){
			botoes.add(new Button());
			botoes.get(i).setText(stringBotoes[i]);
			if((stringBotoes[i].length() < 2) && Character.isDigit(stringBotoes[i].charAt(0)) || stringBotoes[i].equals(","))
				botoes.get(i).setStyle("-fx-text-fill: black;" +
						"    -fx-font-family: 'Consolas';" +
						"	 -fx-font-size: 16px;"	 +	
						"    -fx-background-color: linear-gradient(rgba(220, 230, 230, 0.9), rgba(250,250,250));" + //#61a2b1, #2A5058
						"    -fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.4) , 5, 0.0 , 0 , 0.6 );"); //rgb(250, 250, 249);
			else 
				botoes.get(i).setStyle("-fx-text-fill: black;" +
						"    -fx-font-family: 'Consolas';" +
						"	 -fx-font-size: 12px;"	 +
						"    -fx-background-color: linear-gradient(rgba(180, 200, 210,0.9), rgba(200, 224, 237));" + //#61a2b1, #2A5058
						"    -fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.6) , 5, 0.0 , 0 , 0.6 );");//setStyle("-fx-base: rgb(213, 224, 237);");
			botoes.get(i).setPrefSize(40, 40);
			if(stringBotoes[i].equals("0")) {
				GridPane.setColumnSpan(botoes.get(i), 2);
				botoes.get(i).setPrefWidth(85);
				GridPane.setConstraints(botoes.get(i), x, y, 2, 1);
				x+=2;
				
			} else if(stringBotoes[i].equals("=")) {
				GridPane.setConstraints(botoes.get(i), x, y, 1, 2);
				botoes.get(i).setPrefHeight(90);
				x++;
				
			} else {
				GridPane.setConstraints(botoes.get(i), x, y,1,1);
				x++;
			}
			
			if(x == 5) {
				x = 0;
				y++;
			}
		}
		grid.getChildren().addAll(visor);
		grid.getChildren().addAll(botoes);
		final ObservableList<Operacao> data = FXCollections.observableArrayList(

	            new Operacao("1+2", "3"),

	            new Operacao("2+13", "15"),
	            new Operacao("5+10", "15")


	    );
		expCol = new TableColumn<Operacao , String>();
        expCol.setText("Expressão");
        expCol.setCellValueFactory(new PropertyValueFactory("expressao"));
        expCol.setMinWidth(180);
        resultadoCol = new TableColumn<Operacao, String>();

        resultadoCol.setText("Resultado");

        resultadoCol.setCellValueFactory(new PropertyValueFactory("resultado"));

        resultadoCol.setMinWidth(66);
        tableView = new TableView<Operacao>();
        tableView.setItems(data);

        tableView.getColumns().add(expCol);
        tableView.setPrefSize(250, 150);
		root.getChildren().addAll(menuBar, grid);
		
	}
	
	 public static void main(String[] args) { launch(args); }

}
